package main

import (
	"fmt"
	"third_part/redis/redisclient/client"
	"os"
)

const (
	RedisHost = "192.168.100.203"
	RedisPort = 6379
	RedisNO   = 1
)

type TrackStruct struct {
	objId             uint32
	GpsTime           uint32
	RcvTime           uint32
	Lng               uint32
	Lat               uint32
	Speed             uint8
	Direction         uint8
	Altitude          uint8
	Battery           uint8
	MessageTyep       uint8
	GsmStrength       uint8
	GpsAccuracyType   uint8
	Province          string
	City              string
	Region            string
	TheDayInitMileage uint32
	IsOnline          bool
	Mileage           uint32
}

var (
	RedisPool = redisclient.RedisPool{}
	lineBreak   string = "\n"
	//filename    string = "C:\\Users\\Administrator\\Desktop\\trackrecord.txt"
	filename    string = "/tmp/trackrecord.txt"
)

func WriteLine(info string) error {
	var (
		file *os.File
		err  error
	)
	file, err = os.OpenFile(filename, os.O_WRONLY|os.O_EXCL|os.O_CREATE, 0755)
	if err != nil {
		file, err = os.OpenFile(filename, os.O_WRONLY|os.O_APPEND, 0755)
		if err != nil {
			fmt.Println("error 4 reason:", err.Error())
			return err
		}
	}
	_, err = file.WriteString(info + lineBreak)
	if err != nil {
		fmt.Println("error 5 reason:", err.Error())
		return err
	}
	return nil
}


func getTrack() {
	var (
		trackinfo = ""
	)
	RedisPool.InitRedisClient(RedisHost, RedisPort)

	c := RedisPool.GetRedisClient(RedisNO)
	alltrack, err := c.Keys("track:*")
	if err != nil {
		fmt.Println("operator [keys(\"track\")] err:reason:", err.Error())
	}
	for _, v := range alltrack {
		allelement, err := c.HGetAll(v)
		if err != nil {
			fmt.Println("operator [ HGetAll ] err,reason:", err.Error())
			continue
		}

		for kk, vv := range allelement {
			trackinfo += fmt.Sprintln(kk, ":", vv,",")
		}
		WriteLine(trackinfo)
	}
}
func main() {
	getTrack()
}
